<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <span>分类</span>
      <category-cascader class="queryclass" />
      <span>品牌</span>
      <brand-select class="queryclass" />
      <span>品牌</span>
      <el-input-number v-model="queryParam.priceMin" style="width:160px" :min="0" />-
      <el-input-number v-model="queryParam.priceMax" style="width:160px" :min="0" />

      <el-button type="primary" small style="margin:10px" size="mini" icon="el-icon-search" @click="queryBtn">查询
      </el-button>
      <!--表格-->
      <el-table :data="listForm" stripe border style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            商品标题：{{ scope.row.skuTitle }}
            <br>
            商品副标题：{{ scope.row.skuSubtitle }}
            <br>
            商品描述：{{ scope.row.skuDesc }}
            <br>
            分类ID：{{ scope.row.catalogId }}
            <br>
            SpuID：{{ scope.row.spuId }}
            <br>
            品牌ID：{{ scope.row.brandId }}
            <br>
          </template>
        </el-table-column>
        <el-table-column prop="skuId" header-align="center" align="center" label="skuId" width="50" />
        <el-table-column prop="skuName" header-align="center" align="center" label="sku名称" />
        <el-table-column prop="skuDefaultImg" header-align="center" align="center" label="默认图片">
          <template slot-scope="scope">
            <img :src="scope.row.skuDefaultImg" style="width:80px;height:80px;">
          </template>
        </el-table-column>
        <el-table-column prop="price" header-align="center" align="center" label="价格" width="80" />
        <el-table-column prop="saleCount" header-align="center" align="center" label="销量" width="80" />
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-hasPermi="['admin','product:skuinfo:edit']" type="text" size="mini" @click="editBtn(scope.row.id)">预览</el-button>
            <el-button v-hasPermi="['admin','product:skuinfo:del']" type="text" size="mini" @click="delBtn(scope.row.id)">评论</el-button>
            <el-dropdown
              size="small"
              split-button
              type="text"
              @command="handleCommand(scope.row,$event)"
            >
              更多
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="uploadImages">上传图片</el-dropdown-item>
                <el-dropdown-item command="seckillSettings">参与秒杀</el-dropdown-item>
                <el-dropdown-item command="reductionSettings">满减设置</el-dropdown-item>
                <el-dropdown-item command="discountSettings">折扣设置</el-dropdown-item>
                <el-dropdown-item command="memberPriceSettings">会员价格</el-dropdown-item>
                <el-dropdown-item command="stockSettings">库存管理</el-dropdown-item>
                <el-dropdown-item command="couponSettings">优惠劵</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>

          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        background
        :current-page="pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

    </el-card>

    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="100px">
          <el-form-item label="skuId" prop="skuId">
            <el-input v-model="addForm.skuId" />
          </el-form-item>
          <el-form-item label="spuId" prop="spuId">
            <el-input v-model="addForm.spuId" />
          </el-form-item>
          <el-form-item label="sku名称" prop="skuName">
            <el-input v-model="addForm.skuName" />
          </el-form-item>
          <el-form-item label="sku介绍描述" prop="skuDesc">
            <el-input v-model="addForm.skuDesc" />
          </el-form-item>
          <el-form-item label="所属分类id" prop="catalogId">
            <el-input v-model="addForm.catalogId" />
          </el-form-item>
          <el-form-item label="品牌id" prop="brandId">
            <el-input v-model="addForm.brandId" />
          </el-form-item>
          <el-form-item label="默认图片" prop="skuDefaultImg">
            <el-input v-model="addForm.skuDefaultImg" />
          </el-form-item>
          <el-form-item label="标题" prop="skuTitle">
            <el-input v-model="addForm.skuTitle" />
          </el-form-item>
          <el-form-item label="副标题" prop="skuSubtitle">
            <el-input v-model="addForm.skuSubtitle" />
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="addForm.price" />
          </el-form-item>
          <el-form-item label="销量" prop="saleCount">
            <el-input v-model="addForm.saleCount" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" label-width="100px">
          <el-form-item label="skuId" prop="skuId">
            <el-input v-model="editForm.skuId" />
          </el-form-item>
          <el-form-item label="spuId" prop="spuId">
            <el-input v-model="editForm.spuId" />
          </el-form-item>
          <el-form-item label="sku名称" prop="skuName">
            <el-input v-model="editForm.skuName" />
          </el-form-item>
          <el-form-item label="sku介绍描述" prop="skuDesc">
            <el-input v-model="editForm.skuDesc" />
          </el-form-item>
          <el-form-item label="所属分类id" prop="catalogId">
            <el-input v-model="editForm.catalogId" />
          </el-form-item>
          <el-form-item label="品牌id" prop="brandId">
            <el-input v-model="editForm.brandId" />
          </el-form-item>
          <el-form-item label="默认图片" prop="skuDefaultImg">
            <el-input v-model="editForm.skuDefaultImg" />
          </el-form-item>
          <el-form-item label="标题" prop="skuTitle">
            <el-input v-model="editForm.skuTitle" />
          </el-form-item>
          <el-form-item label="副标题" prop="skuSubtitle">
            <el-input v-model="editForm.skuSubtitle" />
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="editForm.price" />
          </el-form-item>
          <el-form-item label="销量" prop="saleCount">
            <el-input v-model="editForm.saleCount" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit, del, getSkuInfo, listSkuInfo } from '@/api/product/skuinfo'

import CategoryCascader from '@/views/modules/common/category-cascader'
import BrandSelect from '@/views/modules/common/brand-select'
export default {
  name: 'SkuInfo',
  components: { CategoryCascader, BrandSelect },
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 1,
      listForm: [],
      addForm: {
        skuId: '',
        spuId: '',
        skuName: '',
        skuDesc: '',
        catalogId: '',
        brandId: '',
        skuDefaultImg: '',
        skuTitle: '',
        skuSubtitle: '',
        price: '',
        saleCount: ''
      },
      editForm: {},
      addFormRules: {

        skuName: [
          { required: true, message: '请输入sku名称', trigger: 'blur' }
        ],
        skuDesc: [
          { required: true, message: '请输入sku介绍描述', trigger: 'blur' }
        ],
        catalogId: [
          { required: true, message: '请输入所属分类id', trigger: 'blur' }
        ],
        brandId: [
          { required: true, message: '请输入品牌id', trigger: 'blur' }
        ],
        skuDefaultImg: [
          { required: true, message: '请输入默认图片', trigger: 'blur' }
        ],
        skuTitle: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        skuSubtitle: [
          { required: true, message: '请输入副标题', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' }
        ],
        saleCount: [
          { required: true, message: '请输入销量', trigger: 'blur' }
        ]
      },
      queryParam: {
        status: '',
        catalogId: '',
        brandId: '',
        priceMin: 0,
        priceMax: 0
      }
    }
  },
  created() {
    // 初始化列表
    this.initListSkuInfo()
  },
  // 生命周期-挂载完成（可以访问DOM元素）
  mounted() {
    this.PubSub.subscribe('catPath', (msg, val) => {
      this.queryParam.catalogId = val[val.length - 1]
    })
    this.PubSub.subscribe('brandId', (msg, val) => {
      this.brandId = val
    })
  },
  methods: {
    // 处理更多指令
    handleCommand(row, command) {
      console.log('~~~~~', row, command)
      if (command === 'stockSettings') {
        this.$router.push({ path: '/ware-sku', query: { skuId: row.skuId }})
      }
    },
    // 查询按钮
    queryBtn() {
      this.initListSkuInfo()
    },
    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListSkuInfo()
          }
          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      getSkuInfo(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },

    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListSkuInfo()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 删除按钮
    delBtn(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListSkuInfo()
          }
        })
      })
    },
    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.initListSkuInfo()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.initListSkuInfo()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
    },

    // 初始化数据
    initListSkuInfo() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize,
        catalogId: this.queryParam.catalogId,
        brandId: this.brandId,
        publishStatus: this.queryParam.status,
        priceMin: this.queryParam.priceMin,
        priceMax: this.queryParam.priceMax
      }
      listSkuInfo(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    }
  }
}
</script>

<style scoped>
.queryclass{
  display: inline-block;
  margin: 10px;
}

</style>
